//
//  MyNetWorking.m
//  NewProject
//
//  Created by admin on 2017/6/19.
//  Copyright © 2017年 李航飞. All rights reserved.
//

#import "MyNetWorking.h"
#import <AFNetworking/AFNetworking.h>
@interface MyNetWorking ()

@property (nonatomic, strong) AFHTTPSessionManager *manager;

@end

@implementation MyNetWorking
//初始化（单例）
+ (instancetype)sharedManager {
  		static id instance = nil;
  		static dispatch_once_t onceToken;
  		dispatch_once(&onceToken, ^{
            instance = [[super allocWithZone:NULL] init];
        });
  		return instance;
}
//??????
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    return [[self class] manager];
}

- (instancetype)copyWithZone:(struct _NSZone *)zone{
    return [[self class] manager];
}

- (AFHTTPSessionManager *)manager {
    if (!_manager) {
        _manager=[AFHTTPSessionManager manager];
        _manager.responseSerializer.acceptableContentTypes = nil;
        _manager.requestSerializer=[AFHTTPRequestSerializer serializer];
        _manager.responseSerializer=[AFHTTPResponseSerializer serializer];
        _manager.requestSerializer = [AFJSONRequestSerializer serializer];
        _manager.responseSerializer = [AFJSONResponseSerializer serializer];
        _manager.responseSerializer.acceptableContentTypes =  [NSSet setWithObjects:@"application/json", @"text/html",@"text/json",@"text/javascript",@"text/plain", nil];
    }
    return _manager;
}

//get请求
- (void)GET_PATH:(NSString *)path success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    NSString *url = SF(@"%@%@", HOSTURL, path);
    NSString *utf = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"\n[URL]:%@", url);
    [self.manager GET:utf parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *result = [[NSDictionary alloc]initWithDictionary:responseObject];
//        NSDictionary* result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        if ([result[@"status"] isEqualToString:@"1"]) {
            success(result[@"data"]);
        }
        NSLog(@"%@",result[@"msg"]);
//        NSLog(@"\n[Request Success]:\n%@", result);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"\n[Request Failure]:\n[Fail Code]:%ld\n[Fail Message]:%@", (long)error.code, error.userInfo);
        failure(error);
        
    }];
    //    [[HRRequest manager]GET:path para:nil success:^(id data) {
    //        success(data);
    //    } faiulre:^(NSString *errMsg) {
    //    }];
}

//post请求
- (void)POST_PATH:(NSString *)path params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    NSString *url = SF(@"%@%@",HOSTURL, path);
    NSLog(@"\n[URL]:%@", url);
    [self.manager POST:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        NSLog(@"\n[Request Success]:\n%@", result);
        success(result);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"\n[Request Failure]:\n[Failure Code]:%ld\n[Failure Message]:%@", (long)error.code, error.userInfo);
        failure(error);
    }];
}

@end
